﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.Model;//Please add references
namespace OgilvyOne.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:ActivityTitbitsPhotosDal
    /// </summary>
    public partial class ActivityTitbitsPhotosDal : IActivityTitbitsPhotosManage 
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public ActivityTitbitsPhotosDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name,int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_TITBITS_PHOTOS");
            strSql.Append(" where PHOTO_TITLE=@PHOTO_TITLE and PHOTO_ID not in (" + id + ")");
            SqlParameter[] parameters = {
					new SqlParameter("@PHOTO_TITLE",name )
                };         
            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), parameters).ToString()) > 0;
        }

        //PHOTO_ID, TITBITS_ID, PHOTO_TITLE, PHOTO_DESC, PHOTO_IMG_FILE, STATE, USER_ID, ADDTIME
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ActivityTitbitsPhotosManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into COFCO_TITBITS_PHOTOS(");
            strSql.Append(" TITBITS_ID, PHOTO_TITLE, PHOTO_DESC, PHOTO_IMG_FILE, STATE, USER_ID, ADDTIME)");
            strSql.Append(" values (");
            strSql.Append("@TITBITS_ID,@PHOTO_TITLE,@PHOTO_DESC,@PHOTO_IMG_FILE,@STATE,@USER_ID,@ADDTIME)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@TITBITS_ID", SqlDbType.Int,4),
					new SqlParameter("@PHOTO_TITLE", SqlDbType.VarChar,200),
                    new SqlParameter("@PHOTO_DESC", SqlDbType.VarChar,400),
                    new SqlParameter("@PHOTO_IMG_FILE", SqlDbType.VarChar,200),
                    new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@USER_ID", SqlDbType.Int,4),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime)                    
                                        };
            parameters[0].Value = model.TitbitsId;
            parameters[1].Value = model.PhotoTitle;
            parameters[2].Value = model.PhotoDesc;
            parameters[3].Value = model.PhotoImage;
            parameters[4].Value = model.State;           
            parameters[5].Value = model.UserId;
            parameters[6].Value = model.AddTime;
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ActivityTitbitsPhotosManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_TITBITS_PHOTOS set ");
            strSql.Append("TITBITS_ID=@TITBITS_ID,");
            strSql.Append("PHOTO_TITLE=@PHOTO_TITLE,");
            strSql.Append("PHOTO_DESC=@PHOTO_DESC,");
            strSql.Append("PHOTO_IMG_FILE=@PHOTO_IMG_FILE,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("USER_ID=@USER_ID,");
            strSql.Append("ADDTIME=@ADDTIME");
            strSql.Append(" where PHOTO_ID=@PHOTO_ID");
            
            SqlParameter[] parameters = {
					new SqlParameter("@TITBITS_ID", SqlDbType.Int,4),
					new SqlParameter("@PHOTO_TITLE", SqlDbType.VarChar,200),
                    new SqlParameter("@PHOTO_DESC", SqlDbType.VarChar,400),
                    new SqlParameter("@PHOTO_IMG_FILE", SqlDbType.VarChar,200),
                    new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@USER_ID", SqlDbType.Int,4),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime),
                    new SqlParameter("@PHOTO_ID", SqlDbType.Int)
                                        };
            parameters[0].Value = model.TitbitsId;
            parameters[1].Value = model.PhotoTitle;
            parameters[2].Value = model.PhotoDesc;
            parameters[3].Value = model.PhotoImage;
            parameters[4].Value = model.State;
            parameters[5].Value = model.UserId;
            parameters[6].Value = model.AddTime;
            parameters[7].Value = model.PhotoId;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int PHOTO_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_TITBITS_PHOTOS ");
            strSql.Append(" where PHOTO_ID=@PHOTO_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@PHOTO_ID", SqlDbType.Int,4)
};
            parameters[0].Value = PHOTO_ID;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters)>0;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string PHOTO_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_TITBITS_PHOTOS ");
            strSql.Append(" where PHOTO_ID in (" + PHOTO_IDlist + ")  ");
            
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null)>0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ActivityTitbitsPhotosManageModel GetModel(int PHOTO_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PHOTO_ID, TITBITS_ID, PHOTO_TITLE, PHOTO_DESC, PHOTO_IMG_FILE, STATE, USER_ID, ADDTIME from COFCO_TITBITS_PHOTOS ");
            strSql.Append(" where PHOTO_ID="+PHOTO_ID);         

            ActivityTitbitsPhotosManageModel model = new ActivityTitbitsPhotosManageModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PHOTO_ID"].ToString() != "")
                {
                    model.PhotoId = int.Parse(ds.Tables[0].Rows[0]["PHOTO_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TITBITS_ID"].ToString() != "")
                {
                    model.TitbitsId = int.Parse(ds.Tables[0].Rows[0]["TITBITS_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["USER_ID"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["USER_ID"].ToString());
                }
                model.PhotoTitle = ds.Tables[0].Rows[0]["PHOTO_TITLE"].ToString();
                model.PhotoDesc = ds.Tables[0].Rows[0]["PHOTO_DESC"].ToString();
                if (ds.Tables[0].Rows[0]["ADDTIME"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["ADDTIME"].ToString());
                }
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }               
                model.PhotoImage = ds.Tables[0].Rows[0]["PHOTO_IMG_FILE"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PHOTO_ID, TITBITS_ID, PHOTO_TITLE, PHOTO_DESC, PHOTO_IMG_FILE, STATE, USER_ID, ADDTIME from COFCO_TITBITS_PHOTOS ");            
            if (strWhere.Trim() != null & strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" PHOTO_ID, TITBITS_ID, PHOTO_TITLE, PHOTO_DESC, PHOTO_IMG_FILE, STATE, USER_ID, ADDTIME from COFCO_TITBITS_PHOTOS ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_TITBITS_PHOTOS";
            parameters[1].Value = "PHOTO_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

